package com.banshuibao.bsb_android.util;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.MyApplication;
import com.banshuibao.bsb_android.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by zyh on 2017/2/10.
 * <p>
 * 加载图片工具类
 */

public class ImageUtil {
    private static String url = Constants.Url.SHOW_IMAGE;

    /*
    *  使用Glide加载cookieId手动配置GlideUrl
     */
    private static GlideUrl getUrl(String image) {
        return new GlideUrl(url + image, new LazyHeaders.Builder().addHeader("cookieId", MyApplication.applicationInstance.getCookieId()).build());
    }

    /**
     * 加载正常图片
     **/
    public static void loadImage(Context context, String Image_name, ImageView imageView) {
        Glide.with(context).load(getUrl(Image_name)).asBitmap().into(imageView);
    }

    /**
     * 加载图形验证码图片
     **/
    public static void loadNumberImage(Context context, String Image_name, ImageView imageView) {
        Glide.with(context).load(Image_name).asBitmap().placeholder(R.drawable.catch_b).into(imageView);
    }

    /**
     * 加载圆形背景图片
     **/
    public static void loadCircleImage(Context context, String Image_name, ImageView imageView) {
        Glide.with(context).load(getUrl(Image_name)).bitmapTransform(new CropCircleTransformation(context)).into(imageView);
    }

    /**
     * 加载圆角图片
     *
     * @param radius 圆角弧度
     * @param margin 图片margin
     **/
    public static void loadRoundImage(Context context, String Image_name, ImageView imageView, int radius, int margin) {
        Glide.with(context).load(getUrl(Image_name)).bitmapTransform(new RoundedCornersTransformation(context, radius, margin)).into(imageView);
    }

    /**
     * 加载头像
     **/
    public static void loadImageHead(Context context, String Image_name, ImageView imageView) {
        if (TextUtils.isEmpty(Image_name)) {
            imageView.setImageResource(R.drawable.login_iv);
            return;
        }
        Glide.with(context).load(getUrl(Image_name)).placeholder(R.drawable.login_iv)
                .error(R.drawable.login_iv).bitmapTransform(new CropCircleTransformation(context)).into(imageView);
    }

    /**
     * 设置模糊图片
     **/
    public static void loadBlurImage(Context context, String Image_name, ImageView imageView) {
        Glide.with(context).load(getUrl(Image_name)).error(R.drawable.background_black).bitmapTransform(new BlurTransformation(context)).into(imageView);
    }

    /**
     * 加载圆角头像图片图片
     **/
    public static void loadRoundHeaderImage(Context context, String Image_name, ImageView imageView) {
        if (TextUtils.isEmpty(Image_name)) {
            imageView.setImageResource(R.drawable.login_iv);
            return;
        }
        Glide.with(context).load(getUrl(Image_name)).placeholder(R.drawable.login_iv)
                .error(R.drawable.login_iv).centerCrop().bitmapTransform(new RoundedCornersTransformation(context, 20, 0)).into(imageView);
    }

}
